#include "fyp/render/ResourceFactory.h"

//------------------------------------------------------------------------------
// additional header includes
#include "fyp/core/tuple.h"

//------------------------------------------------------------------------------
namespace ming
{
namespace fyp
{
namespace render
{
	//--------------------------------------------------------------------------
	TResourceFactory::TResourceFactory(IDirect3DDevice9 *device) :
		mTexCache( new TD3DTextureLoadingPolicy(device) ),
		mRttCache( new TD3DRenderedTextureLoadingPolicy(device) ),
		mZBufCache( new TD3DDepthBufferLoadingPolicy(device) ),
		mEffectCache( new TD3DXEffectLoadingPolicy(device) ),
		mXMeshCache( new TD3DXMeshLoadingPolicy(device) )
	{
	}
	
	//--------------------------------------------------------------------------
	TResourceFactory::~TResourceFactory()
	{
	}
	
	//--------------------------------------------------------------------------
	IDirect3DTexture9* TResourceFactory::LoadTexture(const char* filename)
	{
		return mTexCache.Load(filename);
	}

	//--------------------------------------------------------------------------
	IDirect3DTexture9* TResourceFactory::LoadRenderedTexture(size_t format, size_t w, size_t h)
	{
		static size_t rtId = 0;

		char id[256];
		sprintf(id, "%d;%d;%d;%d", rtId++, format, w, h);
		return mRttCache.Load(id, Any( core::make_tuple(format, w, h) ) );
	}

	//--------------------------------------------------------------------------
	IDirect3DSurface9* TResourceFactory::LoadDepthBuffer(size_t w, size_t h)
	{
		char id[256];
		sprintf(id, "%d;%d", w, h);
		return mZBufCache.Load(id, Any( core::make_tuple(w, h) ) );
	}

	//--------------------------------------------------------------------------
	ID3DXEffect* TResourceFactory::LoadEffect(const char* filename)
	{
		return mEffectCache.Load(filename);
	}

	//--------------------------------------------------------------------------
	ID3DXMesh* TResourceFactory::LoadXMesh(const char* filename)
	{
		return mXMeshCache.Load(filename);
	}

	//--------------------------------------------------------------------------
	void TResourceFactory::UnloadTexture(IDirect3DTexture9* tex)
	{
		mTexCache.Unload(tex);
	}

	//--------------------------------------------------------------------------
	void TResourceFactory::UnloadRenderedTexture(IDirect3DTexture9* tex)
	{
		mRttCache.Unload(tex);
	}

	//--------------------------------------------------------------------------
	void TResourceFactory::UnloadDepthBuffer(IDirect3DSurface9* buf)
	{
		mZBufCache.Unload(buf);
	}

	//--------------------------------------------------------------------------
	void TResourceFactory::UnloadEffect(ID3DXEffect* effect)
	{
		mEffectCache.Unload(effect);
	}

	//--------------------------------------------------------------------------
	void TResourceFactory::UnloadXMesh(ID3DXMesh* xmesh)
	{
		mXMeshCache.Unload(xmesh);
	}

	//--------------------------------------------------------------------------
}
}
}
